from pkgr.tests.pkgrtestcase import PkgrTestCase
import os
import logging
import solv

logger = logging.getLogger('pkgr.test.test_pkgr')
class Test_Pkg_Basic(PkgrTestCase):
    
    def test_update(self):
        """ Test updating a package to the latest. """
        pkgr = self.pkgr
        
        
        # install the package to update
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('install', ['f-1-1'])
        pkgr.set_jobs(jobs)
        
        for job in jobs:
            job.how = solv.Job.SOLVER_INSTALL
            
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        problems = solver.solve(jobs)
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        del solver
        self.assertTrue(not trans.isempty())
        rpmrunproblems = pkgr.run_transaction(trans)
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        if rpmrunproblems:
            print rpmrunproblems
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'f1')))
        self.dump_install_root()
        
        
        
        
        self.load_new_pkgr()
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('update', ['f'])
        
        print '!!!!!!!', jobs[0].solvables()
        
        
        
        if not jobs:
            print "NO JOBS, prefill with general update"
            sel = pool.Selection_all()
            #if repofilter:
            #   sel.filter(repofilter)
            jobs += sel.jobs(solv.Job.SOLVER_UPDATE)
            
        print 'UPDATE TYPE', solv.Job.SOLVER_UPDATE 
        print 'INSTALL TYPE',solv.Job.SOLVER_INSTALL
        
        for job in jobs:
            if job.isemptyupdate():
                print "GOT EMPTY UPDATE!!"
                job.how ^= solv.Job.SOLVER_UPDATE ^ solv.Job.SOLVER_INSTALL
            
            print 'Loop Job-----'
            print job.pool
            print job.how
            print job.solvables()
                
        pkgr.set_jobs(jobs)    
            
        
            #job.how ^= solv.Job.SOLVER_UPDATE ^ solv.Job.SOLVER_INSTALL
            
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        
        # no problems, show transaction
        trans = solver.transaction()
        
        print "TRANS", trans
        # gc/clear refcounts to the solver
        #del solver
        
        self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.dump_install_root()
        
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'f2')))
        self.assertTrue(not os.path.exists(os.path.join(self.installroot, 'f1')))
        self.dump_install_root()
